package com.backstage.service.impl;

import com.backstage.common.annotation.transaction.ReadOnly;
import com.backstage.dao.UserDao;
import com.backstage.entity.Menu;
import com.backstage.entity.User;
import com.backstage.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Project authority
 * @Package com.backstage.service.impl
 * @Data 2017/8/3下午 5:04.
 * @Aurhor 阮雪峰
 */
@Service("loginService")
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserDao userDao;

    @ReadOnly
    public User queryByLogin(User user){
        return userDao.queryByLogin(user);
    }

    @ReadOnly
    public List<String> queryUserPermission(User user) {
        return userDao.queryUserStringPermission(user);
    }

    @ReadOnly
    public List<Menu> getLoginUserPower(User loginUser) {
        List<Menu> menu_list = userDao.getLoginUserMenuPower(loginUser);
        List<Menu> main_list = userDao.getLoginUserMainPower(loginUser);
        for (Menu menu : menu_list) {
            for (Menu main : main_list) {
                if(menu.getMenu_id() == main.getSuper_id()){
                    menu.getChildren().add(main);
                }
            }
        }
        if(menu_list.size() > 0){
            menu_list.get(0).setSpread(true);
        }
        return menu_list;
    }
}
